<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="../lib/vue.js"></script>
  </head>

  <body>
    <!-- 父组件模板-->
    <div id="app">
      <!-- 通过v-on 事件名监听-->
      <cpn @itemclick="cpnclick"></cpn>
    </div>

    <!--子组件模板-->
    <template id="cpn">
      <div>
        <h2>子组件</h2>
        <button v-for="item in categories" @click="itemclick(item)">
          {{item.name}}
        </button>
      </div>
    </template>

    <script>
      // 子组件
      const cpn = {
        template: '#cpn',
        data() {
          return {
            categories: [
              { id: 'aaa', name: 'Hier' },
              { id: 'bbb', name: '海泽旺' },
              { id: 'ccc', name: '变形金刚' }
            ]
          }
        },
        methods: {
          itemclick(item) {
            console.log(item)
            // 传item值到父组件
            this.$emit('itemclick', item)
          }
        }
      }
      var vm = new Vue({
        el: '#app',
        data: {
          message: '标题'
        },
        methods: {
          cpnclick(item) {
            // 接收子组件传递的值

            console.log('cpnclick', item)
          }
        },
        components: {
          cpn
        }
      })
    </script>
  </body>
</html>
